home *** CD-ROM | disk | FTP | other *** search
/ Acorn User: China / Acorn User China CD-ROM (UK) (Disc A) / Acorn User China CD-ROM (UK) (Disc A).bin / DEMON / DTP / JONIX.ARC / Source_c_jonix < prev    next >
Encoding:
Text File  |  1996-07-17  |  4.8 KB  |  206 lines

  1. /* Jonix 0.01 -- (c) Chris Rutter 1996
  2. */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include "jonix.h"
  9. #include "riscos.h"
  10.  
  11. // #define DEBUG
  12. #undef stdout
  13.  
  14. FILE *stdout;
  15.  
  16. int main (int argc, char *argv[])
  17. {
  18.     char *error;
  19.     jonix_options options;
  20.     FILE *fin, *fout;
  21.     stdout = &(__iob[1]);
  22.     if (paramexist ("--stdout", "-s", argc, argv) == 1)
  23.         redirect (paramt ("--stdout", "-s", argc, argv));
  24.     if (argc < 3) syntax ("Too few arguments.\n");
  25.     fin = fopen (argv[1], "r");
  26.     fout = fopen (argv[2], "w+");
  27.     if (fin == 0)
  28.     {
  29.         fprintf (stdout, "Could not open input file.\n");
  30.         if (fout != 0) fclose (fout);
  31.         exit (0);
  32.     }
  33.     if (fout == 0)
  34.     {
  35.         fprintf (stdout, "Could not open output file.\n");
  36.         if (fin != 0) fclose (fin);
  37.         exit (0);
  38.     }
  39.     #ifdef DEBUG
  40.     fprintf (stdout, "debug: %s->%s\n", argv[1], argv[2]);
  41.     #endif
  42.     options.width = 80;
  43.     options.distance = 20;
  44.     options.rowlength = 6;
  45.     options.newline = 0;
  46.     options.remove = 0;
  47.     setparam (--width,-w,width);
  48.     setparam (--rowlength,-r,rowlength);
  49.     setparam (--distance,-d,distance);
  50.     setparam (--newline,-n,newline);
  51.     setparam (--remove,-v,remove);
  52.     #ifdef riscos
  53.     riscos_initialise ();
  54.     #endif
  55.     error = jonix (fin, fout, &options);
  56.     if (error != 0) fprintf (stdout, error);
  57.     fclose (fin);
  58.     fclose (fout);
  59.     #ifdef riscos
  60.     riscos_shutdown ();
  61.     #endif
  62. }
  63.  
  64. char *jonix (FILE *fin, FILE *fout, jonix_options *options)
  65. {
  66.     char *line, lastc;
  67.     int newline, lines=0;
  68.     line = malloc (MAX_LINE);
  69.     #ifdef DEBUG
  70.     fprintf (stdout, "debug: jonix () called. line at %d\n", *((int *) line));
  71.     fprintf (stdout, "debug: options.width = %d\n", options->width);
  72.     fprintf (stdout, "debug: options.distance = %d\n", options->distance);
  73.     fprintf (stdout, "debug: options.rowlength = %d\n", options->rowlength);
  74.     fprintf (stdout, "debug: options.newline = %d\n", options->newline);
  75.     fprintf (stdout, "debug: options.remove = %d\n", options->remove);
  76.     #endif
  77.     while ((feof(fin) == 0) && (ferror(fin) == 0) && (ferror(fout) == 0))
  78.     {
  79.         newline = 0;
  80.         fgets (line, MAX_LINE, fin);
  81.         if (options->newline == 0) options->newline = line[strlen(line)-1];
  82.         if (strlen(line) > 1) lastc = line[strlen(line)-2];
  83.         else lastc = 0;
  84.         if ((islower(line[0]) != 0) && (isalnum(line[0]) != 0) && (line[0] > 32) && (lastc > 0))
  85.         {
  86.             fseek (fout, -1L, SEEK_CUR);
  87.             if ((fgetc(fout) == 10) || (fgetc(fout) == 13))
  88.             {
  89.                 fseek (fout, -1L, SEEK_CUR);
  90.                 fputc (32, fout);
  91.             }
  92.         }
  93.         if (lastc == 46) newline = -1;
  94.         if (strlen(line) < (options->width - options->distance)) newline = -1;
  95.         if (alphas(line) == 0) newline = -2;
  96.         if (nonalphas(line) > options->rowlength) newline = -2;
  97.         if (strlen(line) == 1) newline = -2;
  98.         if (newline == -2 && lines > 0)
  99.         {
  100.             fseek (fout, -1L, SEEK_CUR);
  101.             fputc (options->newline, fout);
  102.         }
  103.         if (newline == 0) line[strlen(line)-1] = 32;
  104.         if (options->remove != 0)
  105.         {
  106.             if ((alphas(line) == 0) && (strlen(line) > 1))
  107.             fputc (options->newline, fout);
  108.             else fputs (line, fout);
  109.         }
  110.         else
  111.         {
  112.             fputs (line, fout);
  113.             lines++;
  114.         }
  115.         #ifdef riscos
  116.         riscos_poll (fin);
  117.         #endif
  118.     }
  119.     return (0);
  120. }
  121.  
  122. void syntax (char *message)
  123. {
  124.     if (message != 0) fprintf (stdout, message);
  125.     fprintf (stdout, "Syntax: jonix <input file> <output file> <options>\n");
  126.     fprintf (stdout, "Options are --width,-w <width of text> (default=80)\n");
  127.     fprintf (stdout, "            --distance,-d <distance> (default=10)\n");
  128.     fprintf (stdout, "            --rowlength,-r <length> (default=6>\n");
  129.     fprintf (stdout, "            --newline,-n <newline character>\n");
  130.     fprintf (stdout, "            --remove,-v <0/1>\n");
  131.     fprintf (stdout, "            --stdout,-s <file to redirect output to>\n");
  132.     exit (0);
  133. }
  134.  
  135. int paramv (char *longs, char *shorts, int argc, char *argv[])
  136. {
  137.     int p,r;
  138.     for (p=0; p<argc; ++p)
  139.     {
  140.         if ((strcmp(argv[p],shorts) == 0) || (strcmp(argv[p],longs) == 0))
  141.         {
  142.             sscanf (argv[p+1], "%d", &r);
  143.         }
  144.     }
  145.     return (r);
  146. }
  147.  
  148. char *paramt (char *longs, char *shorts, int argc, char *argv[])
  149. {
  150.     int p;
  151.     char r[256];
  152.     for (p=0; p<argc; ++p)
  153.     {
  154.         if ((strcmp(argv[p],shorts) == 0) || (strcmp(argv[p],longs) == 0))
  155.         {
  156.             sscanf (argv[p+1], "%s", r);
  157.         }
  158.     }
  159.     return (r);
  160. }
  161.  
  162. int paramexist (char *longs, char *shorts, int argc, char *argv[])
  163. {
  164.     int p,e;
  165.     for (p=0; p<argc; ++p)
  166.     {
  167.         if ((strcmp(argv[p],shorts) == 0) || (strcmp(argv[p],longs) == 0))
  168.         {
  169.             e = 1;
  170.         }
  171.     }
  172.     return (e);
  173. }
  174.  
  175. int nonalphas (char *text)
  176. {
  177.     int p,nonalphas=0,longest=0;
  178.     for (p=0; p<strlen(text); ++p)
  179.     {
  180.         if (isalnum(text[p]) == 0) nonalphas++;
  181.         else
  182.         {
  183.             if (longest < nonalphas) longest = nonalphas;
  184.             nonalphas = 0;
  185.         }
  186.     }
  187.     if (longest < nonalphas) longest = nonalphas;
  188.     return (longest);
  189. }
  190.  
  191. int alphas (char *text)
  192. {
  193.     int p, alphas=0;
  194.     for (p=0; p<strlen(text); ++p)
  195.     {
  196.         if (isalnum(text[p]) != 0) alphas++;
  197.     }
  198.     return (alphas);
  199. }
  200.  
  201. void redirect (char *file)
  202. {
  203.     stdout = fopen (file, "w");
  204.     if (stdout == 0) stdout = &__iob[1];
  205. }
  206.